﻿using System;
using System.Collections.Generic;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Implements;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Biz
{
    public class HocViBiz
    {
        #region Properties

        private IHocViDao _hocViDao;
        private IHocViDao HocViData
        {
            get { return _hocViDao ?? (_hocViDao = new HocViDao()); }
        }

        #endregion

        #region Constructors

        #endregion

        #region Methods

        public List<HocVi> GetAllHocVis()
        {
            return HocViData.GetAll();
        }

        public object GetHocVis(int startIndex, int pageSize, string sorting, string name)
        {
            try
            {
                int totalRecords;
                var hocVis = HocViData.GetHocVis(startIndex, pageSize, sorting, name, out totalRecords);
                return new { Result = "OK", Records = hocVis, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public HocVi CreateHocVi(HocVi hocVi)
        {
            // validate inputs
            if (hocVi == null) return null;

            return string.IsNullOrEmpty(hocVi.Ten) ? null : HocViData.AddHocVi(hocVi);
        }

        public HocVi GetById(int id)
        {
            return id > 0 ? HocViData.GetById(id) : null;
        }

        public void UpdateHocVi(HocVi hocVi)
        {
            if (hocVi == null) return;

            HocViData.UpdateHocVi(hocVi);
        }

        public object DeleteHocVi(int id)
        {
            try
            {
                HocViData.DeleteHocVi(id);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion
    }
}
